﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PreguntaAlExperto.RulesLayer.Services;
using PreguntaAlExperto.RulesLayer.Entities;
using PreguntaAlExperto.TransporterObjectsFramework;
using System.Text;
using System.IO;

namespace PreguntaAlExperto
{
    public class SiteMapHandler
        : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/xml";
            context.Response.Write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            context.Response.Write("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");

            //Root page, always visible for the Crawler
            context.Response.Write("<url>");
                context.Response.Write("<loc>http://preguntaalexperto.net/</loc>");
                context.Response.Write("<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>");
                context.Response.Write("<changefreq>daily</changefreq>");
                context.Response.Write("<priority>0.5</priority>");
            context.Response.Write("</url>");
            //Root page.

            ArticlesService _service = new ArticlesService();
            foreach (PreguntaAlExperto.RulesLayer.Entities.SiteMap _siteMap in _service.getSiteMap())
            {
                context.Response.Write("<url>");
                    context.Response.Write("<loc>http://preguntaalexperto.net/articles/" + _siteMap.loc + "</loc>");
                    context.Response.Write("<lastmod>" + DateTime.Now.ToString("yyyy-MM-dd") + "</lastmod>");
                    context.Response.Write("<changefreq>weekly</changefreq>");
                    context.Response.Write("<priority>0.5</priority>");
                context.Response.Write("</url>");
            }

            context.Response.Write("</urlset>");

            //Update Robots
            using (StreamWriter sw = new StreamWriter(Path.Combine(context.Server.MapPath("."), "Robots.txt"))) 
            {
                sw.WriteLine("User-agent: *");
                sw.WriteLine("Allow: /");

                foreach (PreguntaAlExperto.RulesLayer.Entities.SiteMap _siteMap in _service.getSiteMap())
                {
                    sw.WriteLine("Allow: /articles/" + _siteMap.loc);
                }

                sw.WriteLine("Disallow: /script/");
                sw.WriteLine("Disallow: /images/");
                sw.WriteLine("Disallow: /Components/");
                sw.WriteLine("Disallow: /bin/");
                sw.WriteLine("Disallow: /App_Themes/");
                sw.WriteLine("Disallow: /App_GlobalResources/");
                sw.WriteLine("Disallow: /dpanel/");
                sw.WriteLine("Disallow: /ferozo/");
                sw.WriteLine("Disallow: /fzadmin/");
            }
        }

        #endregion
    }
}
